#include "GLES2Support.h"
#include "LogManager.h"

namespace PVM
{
	void GLES2Support::SetConfigOption(const std::string& name, const std::string& value)
	{
		ConfigOptionMap::iterator it = mOptions.find(name);
		
		if (it == mOptions.end())
		{
			PVM_EXCEPT("Option named " + name + " does not exist.",
					   "GLES2Support::setConfigOption");
		}
		else
		{
			it->second.currentValue = value;
		}
	}
	
	ConfigOptionMap& GLES2Support::getConfigOptions(void)
	{
		return mOptions;
	}
	
	void GLES2Support::initialiseExtensions(void)
	{
		const GLubyte* pcVer = glGetString(GL_VERSION);
		assert(pcVer && "Problems getting GL version string using glGetString");
		
		std::string tmpStr = (const char*)pcVer;
		LogManager::getSingleton().logMessage("GL_VERSION = " + tmpStr);
		mVersion = tmpStr.substr(0, tmpStr.find(" "));
		
		const GLubyte* pcVendor = glGetString(GL_VENDOR);
		tmpStr = (const char*)pcVendor;
		LogManager::getSingleton().logMessage("GL VENDOR = " + tmpStr);
		mVendor = tmpStr.substr(0, tmpStr.find(" "));
		
		const GLubyte* pcRenderer = glGetString(GL_RENDERER);
		tmpStr = (const char*)pcRenderer;
		LogManager::getSingleton().logMessage("GL_RENDERER = " + tmpStr);
		
		std::stringstream ext;
		std::string str;
		
		const GLubyte* pcExt = glGetString(GL_EXTENSIONS);
		LogManager::getSingleton().logMessage("GL_EXTENSIONS = " + std::string((const char*)pcExt));
		
		assert(pcExt && "Problems getting GL extension string using glGetString");
		
		ext << pcExt;
		while (ext >> str)
		{
			LogManager::getSingleton().logMessage("EXT:" + str);
			extensionList.insert(str);
		}
	}
	
	bool GLES2Support::checkExtension(const std::string& ext) const
	{
		if (extensionList.find(ext) == extensionList.end())
			return false;
		
		return true;
	}
}
